![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
bitcoinjs-lib
Advanced tools
The bitcoinjs-lib npm package is a library for Bitcoin-related operations in JavaScript. It provides tools for creating, signing, and verifying Bitcoin transactions, as well as generating and managing Bitcoin addresses and keys.
Generate a Bitcoin Address
This feature allows you to generate a new Bitcoin address. The code creates a random key pair and derives a Bitcoin address from the public key.
const bitcoin = require('bitcoinjs-lib');
const keyPair = bitcoin.ECPair.makeRandom();
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey });
console.log(address);
Create a Bitcoin Transaction
This feature allows you to create a Bitcoin transaction. The code builds a transaction by adding inputs and outputs, and then signs it with the private key.
const bitcoin = require('bitcoinjs-lib');
const keyPair = bitcoin.ECPair.fromWIF('your-private-key');
const txb = new bitcoin.TransactionBuilder();
txb.addInput('previous-txid', 0); // Add input (previous transaction)
txb.addOutput('recipient-address', 100000); // Add output (recipient address and amount in satoshis)
txb.sign(0, keyPair); // Sign the transaction
const tx = txb.build().toHex();
console.log(tx);
Verify a Bitcoin Transaction
This feature allows you to verify a Bitcoin transaction. The code parses a transaction from its hexadecimal representation and checks if all inputs have valid scripts.
const bitcoin = require('bitcoinjs-lib');
const tx = bitcoin.Transaction.fromHex('transaction-hex');
const txb = bitcoin.TransactionBuilder.fromTransaction(tx);
const isValid = txb.inputs.every(input => input.script && input.script.length > 0);
console.log(isValid);
Bitcore-lib is another JavaScript library for Bitcoin-related operations. It offers similar functionalities to bitcoinjs-lib, such as creating and signing transactions, generating addresses, and managing keys. Bitcore-lib is part of the Bitcore project, which includes additional tools and services for Bitcoin development.
Bcoin is a full Bitcoin node implementation in JavaScript. It provides a comprehensive set of tools for Bitcoin development, including wallet management, transaction creation, and blockchain interaction. Bcoin is more feature-rich compared to bitcoinjs-lib, as it can run as a full node and participate in the Bitcoin network.
Bitcoin-core is a JavaScript library for interacting with Bitcoin Core, the reference implementation of the Bitcoin protocol. It allows developers to communicate with a Bitcoin Core node using RPC calls, enabling functionalities such as transaction creation, address management, and blockchain querying. Unlike bitcoinjs-lib, which is a standalone library, bitcoin-core relies on a running Bitcoin Core node.
The pure JavaScript Bitcoin library for node.js and browsers. Used by over a million wallet users and the backbone for almost all Bitcoin web wallets in production today.
If you are thinking of using the master branch of this library in production, stop. Master is not stable; it is our development branch, and only tagged releases may be classified as stable.
npm install bitcoinjs-lib
var bitcoin = require('bitcoinjs-lib')
If you're familiar with how to use browserify, ignore this and proceed normally. These steps are advisory only, and may not be necessary for your application.
Browserify is assumed to be installed for these steps.
From your repository, create an index.js
file
module.exports = {
base58: require('bs58'),
bitcoin: require('bitcoinjs-lib'),
ecurve: require('ecurve'),
BigInteger: require('bigi'),
Buffer: require('buffer')
}
Install each of the above packages locally
npm install bs58 bitcoinjs-lib ecurve bigi buffer
After installation, use browserify to compile index.js
for use in the browser:
$ browserify index.js --standalone foo > app.js
You will now be able to use <script src="app.js" />
in your browser, with each of the above exports accessible via the global foo
object (or whatever you chose for the --standalone
parameter above).
NOTE: See our package.json for the currently supported version of browserify used by this repository.
NOTE: When uglifying the javascript, you must exclude the following variable names from being mangled: Array
, BigInteger
, Boolean
, Buffer
, ECPair
, Function
, Number
, Point
and Script
.
This is because of the function-name-duck-typing used in typeforce.
Example:
uglifyjs ... --mangle --reserved 'Array,BigInteger,Boolean,Buffer,ECPair,Function,Number,Point'
Definitions for Flow typechecker are available in flow-typed repository.
You can either download them directly from the repo, or with the flow-typed CLI
# npm install -g flow-typed
$ flow-typed install -f 0.27 bitcoinjs-lib@2.2.0 # 0.27 for flow version, 2.2.0 for bitcoinjs-lib version
The definitions are complete and up to date with version 2.2.0. The definitions are maintained by @runn1ng.
The below examples are implemented as integration tests, they should be very easy to understand. Otherwise, pull requests are appreciated.
If you have a use case that you feel could be listed here, please ask for it!
Stefan Thomas is the inventor and creator of this project. His pioneering work made Bitcoin web wallets possible. Daniel Cousens, Wei Lu, JP Richardson and Kyle Drake lead the major refactor of the library from 0.1.3 to 1.0.0.
Since then, many people have contributed. Click here to see the comprehensive list.
We are always accepting of pull requests, but we do adhere to specific standards in regards to coding style, test driven development and commit messages.
Please make your best effort to adhere to these when contributing to save on trivial corrections.
$ npm test
$ npm run-script coverage
BitcoinJS (c) 2011-2016 bitcoinjs-lib contributors
Released under MIT license
2.3.0
added
HDNode.prototype.isNeutered
(#536)HDNode.prototype.derivePath
(#538)HDNode.prototype.derive*
(#539)Transaction.prototype.isCoinbase
(#578)Block.prototype.checkMerkleRoot
(#580)Block.calculateMerkleRoot
(#580)TransactionBuilder.prototype.setVersion
(#599)script.isWitnessPubKeyHashOutput
(#602)script.isWitnessScriptHashOutput
(#602)script.witnessPubKeyHashOutput
(#602)script.witnessScriptHashOutput
(#602)script.witnessScriptHashInput
(#602)fixed
HDNode
but no compatible version found (#550)writePushDataInt
output to adhere to minimal data push policy (#617)FAQs
Client-side Bitcoin JavaScript library
The npm package bitcoinjs-lib receives a total of 122,547 weekly downloads. As such, bitcoinjs-lib popularity was classified as popular.
We found that bitcoinjs-lib demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.